/**
 * 
 */
package commons.internal;

import static commons.Preconditions.checkNotNull;

import java.util.HashMap;
import java.util.Map;

import commons.FileAttributes;
import commons.exception.AttributeNotFoundException;

/**
 * @author armstrong
 * 
 */
public class DefaultFileAttributes implements FileAttributes {

	public static DefaultFileAttributes createDefaultFileAttributes() {

		return new DefaultFileAttributes();
	}

	public static DefaultFileAttributes createDefaultFileAttributes(
			final Map<String, String> attributes) {

		return new DefaultFileAttributes(attributes);
	}

	private final Map<String, String>	attributes;

	private DefaultFileAttributes() {

		attributes = new HashMap<String, String>();
	}

	private DefaultFileAttributes(final Map<String, String> attributes) {
		checkNotNull("attributes must not be null", attributes);

		this.attributes = attributes;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see commons.FileAttributes#exists(java.lang.String)
	 */
	@Override
	public boolean exists(final String attributeName) {
		checkNotNull("attributeName must not be null.", attributeName);
		return attributes.containsKey(attributeName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see commons.FileAttributes#getAttribute(java.lang.String)
	 */
	@Override
	public String getAttribute(final String attributeName)
			throws AttributeNotFoundException {
		checkNotNull("attributeName must not be null", attributeName);

		if (!attributes.containsKey(attributeName)) {
			throw new AttributeNotFoundException("attribute not found: "
					+ attributeName);
		}

		return attributes.get(attributeName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see commons.FileAttributes#removeAttribute(java.lang.String)
	 */
	@Override
	public void removeAttribute(final String attributeName)
			throws AttributeNotFoundException {
		checkNotNull("attributeName must not be null.", attributeName);

		if (attributes.remove(attributeName) == null) {
			throw new AttributeNotFoundException("attribute not found: "
					+ attributeName);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see commons.FileAttributes#setAttribute(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void setAttribute(final String attributeName, final String value) {
		checkNotNull("attributeName must not be null.", attributeName);
		checkNotNull("value must not be null.", value);

		attributes.put(attributeName, value);
	}
}
